package com.code.springboot3.controller;

import com.code.springboot3.common.Result;
import com.code.springboot3.model.dto.AddCategoryDto;
import com.code.springboot3.model.dto.UpdateCategoryDto;
import com.code.springboot3.model.vo.CategoryVo;
import com.code.springboot3.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import com.code.springboot3.validation.CategoryId;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

/**
 * 文章分类管理
 */
@Tag(name = "文章分类相关接口")
@RestController
@RequestMapping("/category")  // 修正这里
public class CategoryController {

    @Autowired
    private CategoryService categoryService;
    /**
     *  新增文章分类
     * @param request token
     * @return 返回的用户信息（脱敏）
     */
    @RequiresAuthentication
    @RequiresRoles("user")
    @Operation(summary = "根据token新增文章分类")
    @PostMapping
    public Result<String> addCategory(@RequestBody AddCategoryDto addCategoryDto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        categoryService.addCategory(addCategoryDto, token);
        return Result.success(null);
    }
    /**
     *  文章分类列表
     * @param request token
     * @return 返回的用户信息（脱敏）
     */
    @RequiresAuthentication
    @Operation(summary = "获取文章分类列表")
    @GetMapping
    public Result<List<CategoryVo>> queryCategoryList(HttpServletRequest request) {
        List<CategoryVo> list = categoryService.queryCategoryList();
        return Result.success(list);
    }
    /**
     *  新增文章分类
     * @param request token
     * @return 返回的用户信息（脱敏）
     */
    @RequiresAuthentication
    @RequiresRoles("user")
    @Operation(summary = "根据token查找该用户设置的分类，再进行修改")
    @PutMapping
    public Result<String> updateCategory(@RequestBody UpdateCategoryDto updateCategoryDto, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        categoryService.updateCategory(updateCategoryDto, token);
        return Result.success(null);
    }

    @Operation(
        summary = "删除文章分类",
        description = "删除指定的文章分类，需要用户登录且具有user角色。只能删除自己创建的分类，且分类下不能有文章。",
        parameters = {
            @Parameter(
                name = "id",
                description = "分类ID",
                required = true,
                schema = @Schema(type = "integer", minimum = "1")
            )
        }
    )
    @RequiresAuthentication
    @RequiresRoles("user")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @ApiResponse(responseCode = "400", description = "参数错误")
    @ApiResponse(responseCode = "403", description = "没有权限")
    @ApiResponse(responseCode = "404", description = "分类不存在")
    @DeleteMapping("/{id}")
    public Result<Void> deleteCategory(
            @PathVariable @CategoryId Integer id,
            HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        categoryService.deleteCategory(id, token);
        return Result.success();
    }
}
